//
//  DiscoverTableViewController.m
//  group14
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 吕志伟. All rights reserved.
//

#import "DiscoverTableViewController.h"

#import "DiscoverTableViewCell.h"

#import "DiscoverOneImageCell.h"


//#import "PublishTypeViewController.h"

#import "PublishViewController.h"

#import "SendModel.h"
#import "UIImageView+WebCache.h"

#import "FriendsSecondTableViewController.h"


@interface DiscoverTableViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

@property (nonatomic, assign) BOOL a;

@property (nonatomic, strong) UIButton *imagePickerButton;

@property (nonatomic, strong) NSMutableArray *arr;

@property (nonatomic, strong) NSData *data;


@end

@implementation DiscoverTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.arr = [NSMutableArray array];
    
    // 导航控制器上面的右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(imagePickerButtonClicked:)];
    
    [self.tableView registerClass:[DiscoverTableViewCell class] forCellReuseIdentifier:@"CELL"];
    
    [self.tableView registerClass:[DiscoverOneImageCell class] forCellReuseIdentifier:@"OneImageCell"];
    
    self.a = YES;
    
    
}

// 视图将要出现前查询表中数据
- (void)viewWillAppear:(BOOL)animated
{
    //AVQuery *query = [AVQuery queryWithClassName:@"sendContentText"];
    
    AVQuery *query = [SendModel query];
    
    [query orderByDescending:@"updatedAt"];
    
    NSArray *arr1 = [query findObjects];
    
    self.arr = [arr1 copy];
    
    [self.tableView reloadData];
    
    //NSLog(@"arr ============  %@",self.arr);
    
}



- (void)imagePickerButtonClicked:(UIButton *)sender
{
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"选取图片" delegate:self
                                               cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机", @"相册", @"仅文字", nil];
    
    [action showFromRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) inView:self.view animated:YES];
}

- (void)setImagePickerController:(UIImagePickerControllerSourceType) source
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    imagePicker.sourceType = source;
    
    imagePicker.delegate = self;
    
    [imagePicker setEditing:YES];
    
    imagePicker.allowsEditing = YES;
    
    // 模态推出视图
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    PublishViewController *publishVC = [[PublishViewController alloc] init];
    
    switch (buttonIndex) {
        case 0:
            [self setImagePickerController:UIImagePickerControllerSourceTypeCamera];
            break;
        case 1:{
            [self setImagePickerController:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        }
            break;
        case 2:
            [self.navigationController pushViewController:publishVC animated:YES];
            break;
        default:
            break;
    }
}


// 选择相册里的图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    // 如果图片编辑之后 需要使用UIImagePickerControllerEditedImage
    // 如果图片没有编辑，只需要获取UIImagePickerControllerOriginalImage
    UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    // UIImagePNGRepresentation 把图片转换成PNG的格式，不压缩 比较大
    // UIImageJPEGRepresentation 把图片转化成JPG格式的，第二的系数填的是一个压缩系数，一般式0.5
    self.data = UIImageJPEGRepresentation(image, 0.5);
    
    [self.imagePickerButton setImage:image forState:UIControlStateNormal];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    PublishViewController *publishVC = [[PublishViewController alloc] init];
    
    publishVC.photoData = _data;
    
    //NSLog(@"data ======== %@",publishVC.photoData);
    
    [self.navigationController pushViewController:publishVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

// 返回多少分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

// 返回多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SendModel *model = self.arr[indexPath.row];
    
    if (model.photoFile == nil) {
        
        DiscoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
        [cell.nameButton setTitle:model.userName forState:UIControlStateNormal];
        
        [cell.nameButton addTarget:self action:@selector(ClickedFrindSecondVC:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.myImageView.image = [UIImage imageNamed:@"iconfont-iconfont14.png"];
        
        cell.contentLabel.text = model.contentStr;
        
        [cell.commentButton addTarget:self action:@selector(commentClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.supportButton addTarget:self action:@selector(supportClick:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.selectionStyle = NO;
        
        return cell;


        
    }else {
        DiscoverOneImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneImageCell" forIndexPath:indexPath];
        
        
        [cell.nameButton setTitle:model.userName forState:UIControlStateNormal];
        
        cell.contentLabel.text = model.contentStr;
        
        [cell.nameButton addTarget:self action:@selector(ClickedFrindSecondVC:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.myImageView.image = [UIImage imageNamed:@"iconfont-iconfont14.png"];
        
        [cell.sendImageView sd_setImageWithURL:[NSURL URLWithString:model.photoFile.url]];
//        [cell.sendImageView sd_setImageWithURL:[NSURL URLWithString:model.photoFile.url] placeholderImage:[UIImage imageNamed:@"iconfont-iconfont14.png"]];
        //cell.sendImageView.image = [UIImage imageWithData:[model.photoFile getData]];
        //[cell.sendImageView sd_setImageWithURL:[NSURL URLWithDataRepresentation:[model.photoFile getData] relativeToURL:nil]];
        
        [cell.commentButton addTarget:self action:@selector(commentClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.supportButton addTarget:self action:@selector(supportClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        cell.selectionStyle = NO;
        
        return cell;

    }
    
    

}

- (void)ClickedFrindSecondVC:(UIButton *)button
{
    UIStoryboard *borad = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    FriendsSecondTableViewController *sencondVC = [borad instantiateViewControllerWithIdentifier:@"SecondVC"];
    
    [self.navigationController pushViewController:sencondVC animated:YES];
    
}

// 点赞、评论button点击事件
- (void)commentClicked:(UIButton *)button
{
    NSLog(@"评论");
    
}
- (void)supportClick:(UIButton *)button
{
    
    if (self.a == YES) {
        [button setImage:[UIImage imageNamed:@"iconfont-dianzan-3"] forState:UIControlStateNormal];
        self.a = NO;
        NSLog(@"点赞");
    } else {
        [button setImage:[UIImage imageNamed:@"iconfont-dianzan-2"] forState:UIControlStateNormal];
        NSLog(@"取消点赞");
        self.a = YES;
    }
  
}

// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SendModel *model = self.arr[indexPath.row];
    
    if (model.photoFile == nil) {
        return 110;
    } else {
        return 220;
    }
}




@end
